package keshe.view;

import keshe.ACCOUNT;
import keshe.MyObjectOutputStream;
import keshe.bank;
import keshe.Exception.IdCardException;
import keshe.Exception.PassWordAgainException;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;

public class RegisterJFrame extends JFrame implements ActionListener ,Runnable, MouseListener {
    private TextField name=new TextField("用户名");
    private TextField password=new TextField("密码");
    private TextField passwordagain=new TextField("确认密码");
    private TextField cardId=new TextField("身份证");
    private TextField bankId=new TextField("银行账户");
    private JLabel Welcome=new JLabel(String.format("%50s","请您进行银行账户注册"));
    private ButtonGroup sexGroup=new ButtonGroup();
    private JRadioButton []Sex=new JRadioButton[2];
    private JButton register=new JButton(new ImageIcon("image/login/注册按钮.png"));
    private bank bank1=new bank();
//    private ACCOUNT account;
    public RegisterJFrame(){
        //注册主界面
        IniteRegister();
        //线程
        Thread t=new Thread(this);
        t.start();
        //添加监听
        addListener();
    }

    private void IniteRegister() {
        this.setSize(400,287);
        this.setLocationRelativeTo(null);
        this.setTitle("银行注册界面");
        this.setDefaultCloseOperation(3);
        this.setAlwaysOnTop(true);
        this.setLayout(null);
        JLabel BankId=new JLabel("随机银行账户:");
        //性别单选按钮组
        JLabel sexLabel=new JLabel("性别");
        sexLabel.setBounds(100,200,25,20);

        Sex[0]=new JRadioButton("男");
        Sex[1]=new JRadioButton("女");
        Sex[0].setBounds(150,200,50,20);
        Sex[1].setBounds(250,200,50,20);
        this.add(Sex[0]);
        this.add(Sex[1]);
        sexGroup.add(Sex[0]);
        sexGroup.add(Sex[1]);
        Sex[0].setSelected(true);


        Welcome.setBounds(100,0,200,17);
        BankId.setBounds(100,20,200,20);
        bankId.setBounds(100,50,200,20);
        //银行类随机银行卡号
        bankId.setText(bank1.createBankCardId());
        //设置文本行不能编辑
        bankId.setEditable(false);
        name.setBounds(100,80,200,20);
        cardId.setBounds(100,110,200,20);
        password.setBounds(100,140,200,20);
        passwordagain.setBounds(100,170,200,20);
        this.add(passwordagain);
        this.add(Welcome);
        this.add(BankId);
        this.add(bankId);
        this.add(password);
        this.add(name);
        this.add(cardId);
        this.add(sexLabel);

        ImageIcon back=new ImageIcon("src/keshe/image/background.jpg");
        JLabel background=new JLabel(back);
        background.setBounds(0,-25,400,300);

        register.setBounds(150,225,100,25);
        this.getContentPane().add(register);
        //去掉点击时的银色边框
        register.setBorderPainted(false);

        this.getContentPane().add(background);
        this.setVisible(true);
    }
    public void run(){
        while (true) {
            String str=Welcome.getText();
            Welcome.setText(str.substring(1)+str.substring(0,1));
            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }
    public void addListener(){
        register.addMouseListener(this);
        register.addActionListener(e->{
            String new_bankId=bankId.getText();
            String new_name=name.getText();
            String new_passWord=password.getText();
            String new_passWordagain=passwordagain.getText();
            String new_Idcard=cardId.getText();
            System.out.println(new_Idcard);
            if(new_Idcard.matches("[1-9](\\d{16})(\\d|X)")){
                System.out.println("true");
            }
            String new_sex;
            if(Sex[0].isSelected()){
                 new_sex="男";
            }else {
                 new_sex="女";
            }
            //创建新的账户
            ACCOUNT account=new ACCOUNT(new_bankId,new_name,new_sex,new_passWord,new_Idcard);
            boolean flash=true;
            try {
                account.createAccount(new_bankId,new_name,new_sex,new_passWord,new_passWordagain,new_Idcard);
            } catch (IdCardException ex) {//身份证不合法
                flash=false;
                showDiglog(ex.getLocalizedMessage());
            }catch (PassWordAgainException ex){//两次密码不一致
                flash=false;
                showDiglog(ex.getLocalizedMessage());
            }
            if(flash){
                showDiglog("注册成功");
                //将account对象用序列化流保存到本地
                MyObjectOutputStream oos;
                try {
                    oos= new MyObjectOutputStream(
                            new FileOutputStream("AccountData/Accounts",true));
                   oos.writeObject(account);
                   oos.close();
                } catch (IOException ex) {
                    throw new RuntimeException(ex);
                }
                this.dispose();
                new LoginJFrame();
            }

        });
    }
    //对话框方法
    public void showDiglog(String messeage){
        JOptionPane.showMessageDialog(this,messeage);
    }

    @Override
    public void actionPerformed(ActionEvent e) {

    }



    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        if(e.getSource()==register){
            register.setIcon(new ImageIcon("image/register/注册按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if(e.getSource()==register){
            register.setIcon(new ImageIcon("image/register/注册按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
